package com.guldan.jaina.provider;

import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.gauthority.GAuthorityDTO;
import com.guldan.jaina.dto.gauthority.GRoleAuthSaveDTO;
import com.guldan.jaina.dto.grole.GRoleDTO;
import com.guldan.jaina.dto.users.UserCacheDTO;
import com.guldan.jaina.dto.users.UsersDTO;
import com.guldan.jaina.manager.GAuthorityManager;
import com.guldan.jaina.manager.GRoleManager;
import com.guldan.jaina.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/11/16
 * Time: 23:50
 */
@Component
public class AuthorityProviderImpl implements IAuthorityProvider {

    @Autowired
    private GAuthorityManager gAuthorityManager;

    @Autowired
    private GRoleManager gRoleManager;

    @Autowired
    private UserManager userManager;

    //获取单个区域，传入：id
    @Override
    public ResponseDTO<GAuthorityDTO> getAuthority(Integer id) {
        ResponseDTO<GAuthorityDTO> responseDTO = new ResponseDTO<>();
        GAuthorityDTO gAuthorityDTO = gAuthorityManager.get(id);
        responseDTO.setData(gAuthorityDTO);
        return responseDTO;
    }

    //获取所有权限菜单，返回树形结构
    @Override
    public ResponseDTO<List<GAuthorityDTO>> listAllAuthority() {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        List<GAuthorityDTO> list = gAuthorityManager.listAllAuthority();
        responseDTO.setData(list);
        return responseDTO;
    }

    //通过角色id，返回树形结构权限菜单，传入roleId
    @Override
    public ResponseDTO<List<GAuthorityDTO>> getAuthorityByRoleId(Integer roleId) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        List<GAuthorityDTO> list = gAuthorityManager.getAuthorityListByRoleId(roleId);
        responseDTO.setData(list);
        return responseDTO;
    }

    //修改某个角色用户的权限列表
    @Override
    public ResponseDTO<List<GAuthorityDTO>> saveAuthByRoleId(GRoleAuthSaveDTO dto) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        List<GAuthorityDTO> list = gAuthorityManager.saveAuthByRoleId(dto);
        responseDTO.setData(list);
        return responseDTO;
    }

    //获取所有的角色列表
    @Override
    public ResponseDTO<List<GRoleDTO>> listAllRole() {
        ResponseDTO<List<GRoleDTO>> responseDTO = new ResponseDTO<>();
        List<GRoleDTO> list = gRoleManager.list();
        responseDTO.setData(list);
        return responseDTO;
    }

    //获取某个角色信息
    @Override
    public ResponseDTO<GRoleDTO> getRole(Integer roleId) {
        ResponseDTO<GRoleDTO> responseDTO = new ResponseDTO<>();
        GRoleDTO gRoleDTO = gRoleManager.get(roleId);
        responseDTO.setData(gRoleDTO);
        return responseDTO;
    }

    //保存，新增某个角色信息
    @Override
    public ResponseDTO<GRoleDTO> saveRole(GRoleDTO dto) {
        ResponseDTO<GRoleDTO> responseDTO = new ResponseDTO<>();
        try {
            responseDTO.setData(gRoleManager.save(dto));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }
        return responseDTO;
    }

    //删除某个角色信息
    @Override
    public ResponseDTO<Boolean> deleteRole(Integer roleId) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        Boolean result = gRoleManager.delete(roleId);
        responseDTO.setData(result);
        return responseDTO;
    }

    //用户登录
    @Override
    public ResponseDTO<UsersDTO> login(String userAccount, String password, String JSESSIONID) {
        ResponseDTO<UsersDTO> responseDTO = new ResponseDTO<>();
        UsersDTO result = userManager.login(userAccount, password, JSESSIONID);
        responseDTO.setData(result);
        return responseDTO;
    }


    //将客户端JSESSIONID注册到redis
    @Override
    public ResponseDTO<UserCacheDTO> registClient(String token, String JSESSIONID) {
        ResponseDTO<UserCacheDTO> responseDTO = new ResponseDTO<>();
        UserCacheDTO result = userManager.registClient(token, JSESSIONID);
        responseDTO.setData(result);
        return responseDTO;
    }


    //通过用户id获取用户所授权的菜单
    @Override
    public ResponseDTO<List<GAuthorityDTO>> getMenuByUserId(Integer userId) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        List<GAuthorityDTO> result = userManager.getMenuByUserId(userId);
        responseDTO.setData(result);
        return responseDTO;
    }

    //通过JSESSIONID获取用户所授权的菜单
    @Override
    public ResponseDTO<List<GAuthorityDTO>> getMenuByJSESSIONID(String JSESSIONID) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        List<GAuthorityDTO> result = userManager.getMenuByJSESSIONID(JSESSIONID);
        responseDTO.setData(result);
        return responseDTO;
    }

    //判断某个JSESSIONID是否已经登录
    @Override
    public ResponseDTO<UserCacheDTO> authentication(String JSESSIONID) {
        ResponseDTO<UserCacheDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.authentication(JSESSIONID));
        return responseDTO;
    }

    //退出登录
    @Override
    public ResponseDTO<Boolean> loginOut(String JSESSIONID) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.loginOut(JSESSIONID));
        return responseDTO;
    }


}
